﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Autorent.Desktop.Model;
using Autorent.Desktop.Model.AutorentMngrServiceReference;

namespace Autorent.Desktop.Modules.Autoes.Services
{
    public class AutoesRepository : AutorentEntitiesContext, IAutoesRepository
    {              

        public ObservableCollection<Auto> GetAutoes()
        {
            try
            {
                foreach (Auto a in Context.Autoes)
                {
                    Context.LoadProperty(a, "AutoClass");
                }
                return new ObservableCollection<Auto>(Context.Autoes);
            }
            catch (Exception)
            {
                throw new AutorentModelException("Autoes loading error occured!");
            }
        }

        public void AddAuto(Auto auto)
        {
            try
            {
                Context.AddToAutoes(auto);
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Add auto error occured!");
            }
        }

        public void DeleteAuto(Auto auto)
        {
            try
            {
                Context.DeleteObject(Context.Autoes.Where(a => a.Auto_ID == auto.Auto_ID).First());
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Delete auto error occured!");
            }
        }

        public void UpdateAuto(Auto auto)
        {
            try
            {
                Context.UpdateObject(Context.Autoes.Where<Auto>(a => a.Auto_ID.Equals(auto.Auto_ID)).First());
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Update auto class error occured!");
            }
        }

    }
}
